package com.wenhua.controller;

import com.wenhua.pojo.Result;
import com.wenhua.pojo.Student;
import com.wenhua.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin(origins = "http://localhost:7050")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/student")
    public Result getStudent(
            /* 当前页,默认1 */
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            /* 每页多少条数据(默认10) */
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            /* 查询字符串(默认空) */
            @RequestParam(value = "selectString", required = false, defaultValue = "") String selectString,
            /* 排序方式(默认desc) */
            @RequestParam(value = "sort", required = false, defaultValue = "desc") String sort
            /* 所有用户都可以查询学生,所以不需要验证权限 */
    ){
        //打印参数
        System.out.println("currentPage:"+currentPage);
        System.out.println("pageSize:"+pageSize);
        System.out.println("sort:"+sort);
        System.out.println("selectString:"+selectString);
        //查询学生
        List<Student> students = studentService.selectStudent(currentPage, pageSize, selectString, sort);

        //查询学生数量
        Integer count = studentService.selectStudentCount(selectString);

        System.out.println("结果");
        System.out.println(students);
        System.out.println("符合要求的数据数量");
        System.out.println(count);

        Map<String ,Object> returnMap = new HashMap<>();
        returnMap.put("students",students);
        returnMap.put("count",count);

        return Result.success(returnMap);
    }

    /* 查询学生出入情况 */
    @GetMapping("/studentMove")
    public Result getStudentMove(
            /* 当前页,默认1 */
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            /* 每页多少条数据(默认10) */
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            /* 查询字符串(默认空) */
            @RequestParam(value = "selectString", required = false, defaultValue = "") String selectString,
            /* 排序方式(默认desc) */
            @RequestParam(value = "sort", required = false, defaultValue = "desc") String sort
            /* 所有用户都可以查询学生,所以不需要验证权限 */
    ){
        //打印参数
        System.out.println("currentPage:"+currentPage);
        System.out.println("pageSize:"+pageSize);
        System.out.println("sort:"+sort);
        System.out.println("selectString:"+selectString);
        //查询学生
        List<Student> students = studentService.selectStudentMove(currentPage, pageSize, selectString, sort);

        //查询学生数量
        Integer count = studentService.selectStudentMoveCount(selectString);

        System.out.println("结果");
        System.out.println(students);
        System.out.println("符合要求的数据数量");
        System.out.println(count);

        Map<String ,Object> returnMap = new HashMap<>();
        returnMap.put("students",students);
        returnMap.put("count",count);

        return Result.success(returnMap);
    }

    /* 通过id查询学生 */
    @GetMapping("/student/{id}")
    public Result getStudentById(@PathVariable("id") Integer id){
        System.out.println("id:"+id);
        Student student = studentService.selectStudentById(id);
        return Result.success(student);
    }

    /* 添加学生 */
    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody Student studentInfo){
        //添加学生
        studentService.addStudent(studentInfo);
        System.out.println("添加学生");
        System.out.println(studentInfo);
        return Result.success();
    }

    //修改学生
    @PostMapping("/updateStudent")
    public Result updateStudent(@RequestBody Student studentInfo){
        System.out.println("更新学生数据");
        System.out.println(studentInfo);

        //调用服务
        studentService.updateStudent(studentInfo);

        return Result.success();
    }

        //删除学生/**/
    @PostMapping("deleteStudent")
    public Result deleteStudent(@RequestBody Student studentInfo){
        System.out.println("删除学生数据");
        System.out.println(studentInfo);

        //调用服务
        studentService.deleteStudent(studentInfo.getStudentId());

        return Result.success();
    }

    /*  学生外出 */
    @PostMapping("/studentOut")
    public Result studentOut(@RequestBody Student studentInfo){
        System.out.println("学生外出");
        System.out.println(studentInfo);

        //调用服务
        studentService.studentMove(studentInfo.getStudentId(),"外出");

        return Result.success();
    }

    /* 学生回来 */
    @PostMapping("/studentBack")
    public Result studentBack(@RequestBody Student studentInfo){
        System.out.println("学生回来");
        System.out.println(studentInfo);

        //调用服务
        studentService.studentMove(studentInfo.getStudentId(),"回来");

        return Result.success();
    }



}
